package com.atguigu.tingshu.dispatch.job;

import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.PipedReader;

@Slf4j
@Component
public class DispatchHandler {

    @XxlJob("hellojob")
    public void hellojob() {
        log.info("hellojob..." + DateUtil.now());
        //1.获取调度管理页面参数
        String jobParam = XxlJobHelper.getJobParam();
        if ("error".equals(jobParam)) {
            int i = 1 / 0;
            log.error("hellojob...error");
        }
    }


    /**
     * 分片广播任务
     */
    @XxlJob("shardJob")
    public void shardJob(){
        log.info("分片任务执行.分片参数，总分片数：{}， 当前分片索引：{}", XxlJobHelper.getShardTotal(), XxlJobHelper.getShardIndex());
    }

    @Autowired
    private SearchFeignClient searchFeignClient;

    /**
     * 任务逻辑：远程调用搜索服务更新排行榜数据
     */
    @XxlJob("updateAlbumRanking")
    public void updateAlbumRanking() {
        log.info("[定时任务]远程调用搜索服务更新排行榜数据");
        searchFeignClient.updateLatelyAlbumRanking();
    }


    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 任务逻辑：远程调用用户服务更新VIP标识
     */
    @XxlJob("updateVipExpireStatus")
    public void updateVipExpireStatus() {
        log.info("[定时任务]远程调用用户服务更新VIP标识");
        userFeignClient.updateVipExpireStatus();
    }
}
